package com.zr.d_c.util;
/**
 * 分页的工具类
 * @author Administrator
 *
 */
public class PagingUtil {
	/**
	 * 生成餐厅页码
	 * 
	 * @param totalNum
	 *            传入数据的总数
	 * @param currentPage
	 *            传入当前的页码
	 * @param pageSize
	 *            传入 需要按照多少条来分
	 * @return
	 */
	public static String getPage(int totalNum, int currentPage, int pageSize) {
		if(totalNum==0){
			return "<h3>无商家</h3>";
		}else{
			int totalPage = totalNum % pageSize == 0 ? totalNum / pageSize : totalNum / pageSize + 1;
			StringBuffer page = new StringBuffer();
			page.append("<li><a href='getRestaurant?page=1'>首页</a></li>");
			if (currentPage == 1) {
				page.append("<li class='disabled'><a href='#'>上一页</a></li>");
			} else {
				page.append("<li><a href='getRestaurant?page=" + (currentPage - 1) + "'>上一页</a></li>");
			}
			// 根据当前页码来生产相邻页码
			for (int i = currentPage - 2; i < currentPage + 2; i++) {
				if (i < 1 || i > totalPage) {
					continue;
				}
				if (i == currentPage) {
					page.append("<li class='active'><a href='#'>" + i + "</a></li>");
				} else {
					page.append("<li><a href='getRestaurant?page=" + i + "'>" + i + "</a></li>");
				}
			}
			if (currentPage == totalPage) {
				page.append("<li class='disabled'><a href='#'>下一页</a></li>");
			} else {
				page.append("<li><a href='getRestaurant?page=" + (currentPage + 1) + "'>下一页</a></li>");
			}

			page.append("<li><a href='getRestaurant?page=" + totalPage + "'>尾页</a></li>");
			return page.toString();
		}
		
	}
	/**
	 * 模糊查询生成餐厅页码
	 * 
	 * @param totalNum
	 *            传入数据的总数
	 * @param currentPage
	 *            传入当前的页码
	 * @param pageSize
	 *            传入 需要按照多少条来分
	 * @return
	 */
	public static String getPageByKey(int totalNum, int currentPage, int pageSize,String key) {
		if(totalNum==0){
			return "<h3>查询无此商家</h3>";
		}else{
			int totalPage = totalNum % pageSize == 0 ? totalNum / pageSize : totalNum / pageSize + 1;
			StringBuffer page = new StringBuffer();
			page.append("<li><a href='getRestaurant?page=1&key="+key+"'>首页</a></li>");
			if (currentPage == 1) {
				page.append("<li class='disabled'><a href='#'>上一页</a></li>");
			} else {
				page.append("<li><a href='getRestaurant?page=" + (currentPage - 1) + "&key="+key+"'>上一页</a></li>");
			}
			// 根据当前页码来生产相邻页码
			for (int i = currentPage - 2; i < currentPage + 2; i++) {
				if (i < 1 || i > totalPage) {
					continue;
				}
				if (i == currentPage) {
					page.append("<li class='active'><a href='#'>" + i + "</a></li>");
				} else {
					page.append("<li><a href='getRestaurant?page=" + i + "&key="+key+"'>" + i + "</a></li>");
				}
			}
			if (currentPage == totalPage) {
				page.append("<li class='disabled'><a href='#'>下一页</a></li>");
			} else {
				page.append("<li><a href='getRestaurant?page=" + (currentPage + 1) + "&key="+key+"'>下一页</a></li>");
			}
			
			page.append("<li><a href='getRestaurant?page=" + totalPage + "&key="+key+"'>尾页</a></li>");
			return page.toString();
		}
	}
	/**
	 * 生成菜品页码
	 * 
	 * @param totalNum
	 *            传入数据的总数
	 * @param currentPage
	 *            传入当前的页码
	 * @param pageSize
	 *            传入 需要按照多少条来分
	 * @return
	 */
	public static String getDishPage(int totalNum, int currentPage, int pageSize,int sid) {
		if(totalNum==0){
			return "<h3>无菜品</h3>";
		}else{
			int totalPage = totalNum % pageSize == 0 ? totalNum / pageSize : totalNum / pageSize + 1;
			StringBuffer page = new StringBuffer();
			page.append("<li><a href='bookDish?sid="+sid+"&page=1'>首页</a></li>");
			if (currentPage == 1) {
				page.append("<li class='disabled'><a href='#'>上一页</a></li>");
			} else {
				page.append("<li><a href='bookDish?sid="+sid+"&page=" + (currentPage - 1) + "'>上一页</a></li>");
			}
			// 根据当前页码来生产相邻页码
			for (int i = currentPage - 2; i < currentPage + 2; i++) {
				if (i < 1 || i > totalPage) {
					continue;
				}
				if (i == currentPage) {
					page.append("<li class='active'><a href='#'>" + i + "</a></li>");
				} else {
					page.append("<li><a href='bookDish?sid="+sid+"&page=" + i + "'>" + i + "</a></li>");
				}
			}
			if (currentPage == totalPage) {
				page.append("<li class='disabled'><a href='#'>下一页</a></li>");
			} else {
				page.append("<li><a href='bookDish?sid="+sid+"&page=" + (currentPage + 1) + "'>下一页</a></li>");
			}
			
			page.append("<li><a href='bookDish?sid="+sid+"&page=" + totalPage + "'>尾页</a></li>");
			return page.toString();
		}
	}
	/**
	 * 模糊查询生成菜品页码
	 * 
	 * @param totalNum
	 *            传入数据的总数
	 * @param currentPage
	 *            传入当前的页码
	 * @param pageSize
	 *            传入 需要按照多少条来分
	 * @return
	 */
	public static String getDishPageByKey(int totalNum, int currentPage, int pageSize,String dname,int sid) {
		if(totalNum==0){
			return "<h3>查询无此菜品</h3>";
		}else{
			int totalPage = totalNum % pageSize == 0 ? totalNum / pageSize : totalNum / pageSize + 1;
			StringBuffer page = new StringBuffer();
			page.append("<li><a href='bookDish?sid="+sid+"&page=1&dishname="+dname+"'>首页</a></li>");
			if (currentPage == 1) {
				page.append("<li class='disabled'><a href='#'>上一页</a></li>");
			} else {
				page.append("<li><a href='bookDish?sid="+sid+"&page=" + (currentPage - 1) + "&dishname="+dname+"'>上一页</a></li>");
			}
			// 根据当前页码来生产相邻页码
			for (int i = currentPage - 2; i < currentPage + 2; i++) {
				if (i < 1 || i > totalPage) {
					continue;
				}
				if (i == currentPage) {
					page.append("<li class='active'><a href='#'>" + i + "</a></li>");
				} else {
					page.append("<li><a href='bookDish?sid="+sid+"&page=" + i + "&dishname="+dname+"'>" + i + "</a></li>");
				}
			}
			if (currentPage == totalPage) {
				page.append("<li class='disabled'><a href='#'>下一页</a></li>");
			} else {
				page.append("<li><a href='bookDish?sid="+sid+"&page=" + (currentPage + 1) + "&dishname="+dname+"'>下一页</a></li>");
			}
			
			page.append("<li><a href='bookDish?sid="+sid+"&page=" + totalPage + "&dishname="+dname+"'>尾页</a></li>");
			return page.toString();
		}
	}
}
